// i.r(t),
//   i.d(t, {
//     SettingPersistence: () => p,
//     default: () => SettingsModule
//   })
import Logger from "../utils/Logger"

import Module from "./Module"
import SettingsData from "../data/SettingsData"
import browserUtil from "../utils/browser.util"
import localStorage from "../utils/localStorage"
import settingEnum from "../enum/setting.enum"

const u = new Logger("settings")

export default class SettingsModule extends Module {
  queuedControls: any[]
  queuedButtons: any[]
  secretWord: number[]
  engine: any
  settings: SettingsData

  constructor() {
    super(...arguments), (this.name = "settings")
    this.queuedControls = []
    this.queuedButtons = []
    this.secretWord = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 72]
  }

  async init(e, t) {
    this.engine = t
    this.settings = new SettingsData()
    this.engine.market.register(this, SettingsData, this.settings)
    const urlConfig = browserUtil.queryToMap()
    for (const t in urlConfig) {
      let val = urlConfig[t]
      if ("string" === typeof val && /^\d+$/.test(val)) {
        const e = Number.parseFloat(val)
        isNaN(e) || (val = e)
      }
      this.settings.setProperty(t, val)
    }
  }

  updateSetting(e, t) {
    t && t.clone && "function" == typeof t.clone ? this.settings.setProperty(e, t.clone()) : this.settings.setProperty(e, t)
  }
  updateSettings(e) {
    for (const t in e) this.updateSetting(t, e[t])
  }
  registerSetting(t, i, s = settingEnum.NONE) {
    const h = s === settingEnum.LOCAL_STORAGE ? localStorage.getItem(t, i) : i
    this.updateSetting(t, h)
  }
  registerMenuEntry(e) {
    const { setting: i, initialValue: n, persist: s, onChange: l, urlParam: u } = e
    let p = n()
    const m = encodeURIComponent(i)
    if (u) {
      let e = browserUtil.queryToMap()[m]
      void 0 !== e && (Number.isNaN(Number.parseFloat(e)) ? ("true" === e ? (e = !0) : "false" === e && (e = !1)) : (e = Number.parseFloat(e)), (p = e))
    }
    this.registerSetting(i, p, s)
    if (!l && !u) return null
    if ((l && l(p), u)) {
      const e = n()
      const t = t => {
        const i = browserUtil.queryToMap()
        void 0 !== i[m] && t === e ? delete i[m] : (i[m] = t), browserUtil.routerGo(i, !0), l && l(t)
      }
      return this.settings.onPropertyChanged(i, t)
    }
    return l ? this.settings.onPropertyChanged(i, l) : null
  }
  hasProperty(e) {
    return this.settings.hasProperty(e)
  }
  getProperty(e) {
    return this.settings.getProperty(e)
  }
  tryGetProperty(e, t) {
    return this.settings.tryGetProperty(e, t)
  }
}
